package GUI;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import weavingdraft.WeavingDraft;
import weavingdraft.WeavingDraftFrame;

import Background.Contestant;
import Background.Entry;
import Background.Lists;

/**
 * A class to display the registration window in the gui.  Builds the window and displays it.
 * 
 * @author Chris Petcher
 * @version 27 February, 2013
 *
 */
@SuppressWarnings("serial")
public class SubmitEntryGUI extends JFrame
{
  
	/**
	 * A dimension used in setting the size of the frame.
	 */
	
	private static final Dimension MY_SIZE = new Dimension(1000, 500);
	
	/**
	 * A dimension used in setting the size of the frame.
	 */
	
	private static final Dimension MY_DESCRIPTION_SIZE = new Dimension(500, 300);

	/**
	 * A combo box for the first category.
	 */
	
	private JComboBox category;
	
	
	/**
	 * An array of strings to hold the combo box selections. 
	 */
	
	private final String[] selectionStrings = {"", "Blankets", "Clothing", "Tapestry", "Art", "Custom" };

	/**
	  * This field is for the Border Layout that contains all my buttons for the submit design stage.
	  */

	public Container my_user_card_panel;
	
	/**
	 * A panel to go into the center of the borderlayout for the user card.
	 */
	
	public JPanel my_center_panel;
	
	/**
	  * This field is for the Border Layout that contains all my buttons
	  * for the release design stage.
	  */

	public Container my_release_panel;
	
	private WeavingDraft draft;
	
	private WeavingDraftFrame frame;

	/**
	 * A radiobutton that determines if a user wants thier design released. 
	 */
	
	private JRadioButton accept;
	
	/**
	 * A radiobutton that determines if a user does not want to submit approval.
	 */
	
	private JRadioButton deny;
	
	/**
	 * A boolean that determines if the release was accepted.
	 */
	
	public boolean acceptStatus = false;
	
	/**
	 * A textfield to hold the description for the contestant's entry.
	 */
	
	private JTextField description;

	/**
	 * Constructor for the SubmitEntryGUI.  Sets up the panels for use in the entry submission
	 * process.
	 */
	
	public SubmitEntryGUI() {
		 
	    super("Submit an Entry");
		category = new JComboBox(selectionStrings);
	    my_release_panel = new JPanel(new BorderLayout());
	    my_user_card_panel = new JPanel(new BorderLayout());
	    my_center_panel = new JPanel(new GridLayout(3,1));
		accept = new JRadioButton("I wish to release my design to the Sponsors.");
		deny = new JRadioButton("I do not wish to release my design to the Sponsors.");
		description = new JTextField();
		description.setPreferredSize(MY_DESCRIPTION_SIZE);
		draft = new WeavingDraft();

	  }

	/**
	 * 	Method that brings up the entry submission window and sets up the GUI.
	 */
		public void start() {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		    setVisible(true);
		    setPreferredSize(MY_SIZE);
		    createUserCard();
		    validate();
		    pack();
		}
		
		/**
		 *  Method that returns a reference to the frame we are using.
		 *  
		 * @return the current frame
		 */
		public JFrame getFrame() {
			return this;
		}

		/**
		 * Method that creates the necessary buttons for the release panel
		 */
		
		public void createUserCard() {
			  final JButton next = new JButton("Next");
			  final JLabel descriptionBoxLabel = new JLabel("Please enter a brief description " +
			  		"such as the fibers in the weave, and any other details for the design " +
			  		"such as handspun or self-dyed yarn.");
			  next.addActionListener(new UserCardNextListener(this));
			  my_user_card_panel.add(my_center_panel, BorderLayout.CENTER);
			  my_center_panel.add(descriptionBoxLabel);
			  my_center_panel.add(description);
			  my_center_panel.add(category);
			  my_user_card_panel.add(next, BorderLayout.SOUTH);
			  add(my_user_card_panel);
			  my_user_card_panel.setVisible(true);
			  
			  
			
		}
		
		/**
		 * Method that creates the necessary buttons for the release panel
		 */
		
		public void createReleaseGrid() {
			  final JButton submit = new JButton("Submit");
			  submit.addActionListener(new SubmitEntryListener());
			  my_release_panel.add(accept, BorderLayout.NORTH);
			  my_release_panel.add(deny, BorderLayout.WEST);
		      my_release_panel.add(submit, BorderLayout.SOUTH);
		      accept.addActionListener(new ActionListener()

		        {
		          @Override
				public void actionPerformed(final ActionEvent the_event)
		        
		          {
		        	  accept.setSelected(true);
		        	  deny.setSelected(false);
		        	  acceptStatus = true;
		        	  
		          }

		        });

		      deny.addActionListener(new ActionListener()

	        {
	          @Override
			public void actionPerformed(final ActionEvent the_event)
	        
	          {
	        	  accept.setSelected(false);
	        	  deny.setSelected(true);
	        	  acceptStatus = false;
	          }

	        });
		      
			remove(my_user_card_panel);
			add(my_release_panel);
			validate();
		}
		
		/**
		 * Inner class to go to the next window in the registration process when  button is pressed.
		 * @author Chris Petcher
		 */
		
		class UserCardNextListener implements ActionListener {
			
			private SubmitEntryGUI my_entry;
			
			/**
			 * Action performed method that displays the about message.
			 * @param the_event The event, ignored.
			 */
			
			public UserCardNextListener(SubmitEntryGUI the_entry){
				my_entry = the_entry;
			
			}	
			@Override
			public void actionPerformed(final ActionEvent the_event) 
			{
				Contestant c = SignInGUI.getCurrentContestant();
				Entry e1 = c.getEntry1();
				Entry e2 = c.getEntry2();
				Entry e3 = c.getEntry3();
				
				if (category.getSelectedItem().equals("")) {
					JOptionPane.showMessageDialog(my_user_card_panel, "You must select a category.");
					
				}else if(e1.getCategory().equals(category.getSelectedItem().toString()) || e2.getCategory().equals(category.getSelectedItem().toString()) ||
						e3.getCategory().equals(category.getSelectedItem().toString())){
					JOptionPane.showMessageDialog(my_user_card_panel, "Category already submitted!");
				} else {
					
					frame = new WeavingDraftFrame(my_entry, draft);
					frame.start();
					validate();
				}

			}

		}

	/**
	 * Inner class to submit the entries when the button is pressed.
	 * @author Chris Petcher
	 */
	class SubmitEntryListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			Contestant c = SignInGUI.getCurrentContestant();
			ArrayList<Entry> contestantEntryList = c.getEntryList();
			
			if(contestantEntryList.size() == 0){
			
				Entry first_entry = c.getEntry1();
				first_entry.setDescription(description.getText());
				first_entry.setCategory((String)category.getSelectedItem());
				first_entry.setDesign(draft.getdraft());
				first_entry.setRelease(acceptStatus);
				c.addEntryToList(first_entry);
				Lists.saveData();
				JOptionPane.showMessageDialog(null, "Thank you for submitting an entry! You have 2 entries left!");
				dispose();
			}
			
			else if(contestantEntryList.size() == 1){
				Entry second_entry = c.getEntry2();
				second_entry.setDescription(description.getText());
				second_entry.setCategory((String)category.getSelectedItem());
				second_entry.setDesign(draft.getdraft());
				second_entry.setRelease(acceptStatus);
				c.addEntryToList(second_entry);
				Lists.saveData();
				JOptionPane.showMessageDialog(null, "Thank you for submitting an entry! You have 1 entry left!");
				dispose();
			}

			else if(contestantEntryList.size() == 2){
				Entry third_entry = c.getEntry3();
				third_entry.setDescription(description.getText());
				third_entry.setCategory((String)category.getSelectedItem());
				third_entry.setDesign(draft.getdraft());
				third_entry.setRelease(acceptStatus);
				c.addEntryToList(third_entry);
				Lists.saveData();
				JOptionPane.showMessageDialog(null, "Thank you for submitting an entry! " +
						"You have submitted the maximum number of 3 entries.");
				dispose();
			}

			else{
				JOptionPane.showMessageDialog(null, "You've already submitted 3 entries!");
			}
			
			
		}// close actionperformed

	}// close listener class

}// close SignInGUI class